package brac;

import java.util.ArrayList;

import fn.Function;

public class FalsePosition extends Bracketing {

	public FalsePosition() {
		set();
	}

	public int[] fromTo() {
		return new int[] { 0, 2 };
	}

	@SuppressWarnings("unchecked")
	public void set() {
		String[] s = { "a", "b", "c", "f(c)" };
		header = s;
		values = new ArrayList[4];
	}

	public void solve(Function fn, double presition, int num_iteration, double a, double b) {
		double fa = feval(fn, a);
		double fb = feval(fn, b);
		ArrayList<Double> al = new ArrayList<Double>();
		ArrayList<Double> bl = new ArrayList<Double>();
		ArrayList<Double> cl = new ArrayList<Double>();
		ArrayList<Double> fckl = new ArrayList<Double>();

		double c = 0;
		double fc = 0;
		double dx = 0;
		for (int i = 0; i < num_iteration; i++) {
			dx = (fb * (b - a)) / (fb - fa);
			c = b - dx;
			fc = feval(fn, c);
			al.add(a);
			bl.add(b);
			cl.add(c);
			fckl.add(fc);
			if (Math.abs(fc) <= presition) {
				a = b = c;
				break;
				// return ;
			} else {
				if (fb * fc > 0) {
					b = c;
					fb = fc;
				} else {
					a = c;
					fa = fc;
				}
			}

		}
		values[0] = al;
		values[1] = bl;
		values[2] = cl;
		values[3] = fckl;

	}
	
	@Override
	public String toString() {
		return "False Position";
	}

}
